<?php
class LeagueEditor extends FormField
{
	private $controller = null;
	
	function __construct($controller, $name, $title = null, $value = null, $form = null, $rightTitle = null) {
		$this->controller = $controller;
		parent::__construct($name, $title, $value, $form, $rightTitle);
	}
	
	public function FieldHolder()
	{
		
		Requirements::css("leagueManager/css/FieldEditor.css");
		return $this->renderWith("LeagueEditor");
	}
	
	public function saveInto(DataObject $record) 
	{
		//$_POST['teamLeague-1'];
	}
	
	public function addLeague() {
		
	}
	
	public function Leagues() 
	{
		Requirements::javascript("leagueManager/javascript/LeagueEditor.js");
		
		$leagues = $this->controller->Leagues();
		if ($this->isReadonly()) {
			foreach ($leagues AS $league) {
				$league->isReadonly = true;
			}
		}
		
		return $leagues;
	}
	
}

class LeagueEditor_Controller extends Controller {
	
	
	public function getLeagueTable($id,$isSeason=false) {
		if ($isSeason) {
		    $seasonId = $id;
		} else {
			$league = DataObject::get_one('League','`League`.ID = ' . $id);
			$seasonId = $league->SeasonID;
		}
		
        $season = DataObject::get_one('Season','`Season`.ID = ' . $seasonId,'Name ASC');
        $dos = $season->Leagues();
        $ret = '';
        foreach ($dos AS $league) {
            $league->isReadonly = $season->SeasonStarted;
            $ret .= $league->TeamTable();
        }
        if ($ret == '')$ret = 'no leagues';
        return $ret;
	}
	
	/**
	 * @param HTTPRequest $request
	 * @return String
	 */
	public function addLeague($request)
	{
		$seasonId = $_POST['id'];
		$leagueName = $_POST['name'];
		$season = DataObject::get_one('Season','`Season`.ID = ' . $seasonId,'Name ASC');
		$dos = $season->Leagues();
		if ($leagueName == '')
			$leagueName = 'Liga ' . ($dos->Count() + 1);
		$newLeague = new League();
		$newLeague->SeasonID = $seasonId;
		$newLeague->Name = $leagueName;
		$newLeague->write();
        return $this->getLeagueTable($seasonId,true);
	}
	
	/**
	 * @param HTTPRequest $request
	 * @return String
	 */
	public function addTeamToLeague($request)
	{
		$leagueId = intval($_POST['leagueId']);
		$teamId = intval($_POST['teamId']);
		
		$league = DataObject::get_one('League','`League`.ID = ' . $leagueId);
		$dos = $league->Teams();
	
		foreach ($dos AS $team)
			if ($team->ID == $teamId)
				return '';
		
		$team = DataObject::get_one('Team','`Team`.ID = ' . $teamId);
		DB::query('INSERT INTO League_Teams (LeagueID,TeamID) VALUES('.$leagueId.','.$teamId.')');
		
		$season = DataObject::get_one('Season','`Season`.ID = ' . $league->SeasonID,'Name ASC');
        return $this->getLeagueTable($league->SeasonID,true);
	}
	
	public function deleteLeague($request) {
		$leagueId = intval($_POST['leagueId']);
		$league = DataObject::get_one('League','`League`.ID = ' . $leagueId);
		$seasonId = $league->SeasonID;
		$league->delete();
        return $this->getLeagueTable($seasonId,true);		
		
	}
	
	public function removeTeamFromLeague($request) {
		$leagueId = intval($_POST['leagueId']);
		$teamId = intval($_POST['teamId']);
		DB::query('DELETE FROM League_Teams WHERE LeagueID = '.$leagueId.' AND teamID='.$teamId);
        return $this->getLeagueTable($leagueId);	
	}
	

    public function addFoulPointsToTeam($request) {
    	$leagueId = intval($_POST['leagueId']);
        $teamId = intval($_POST['teamId']);
        $fp = new FoulPoint();
        $fp->TeamID = $teamId;
        $fp->LeagueID = $leagueId;
        $fp->points = $_POST['points'];
        $fp->desc = $_POST['desc'];
        $fp->write();
        $league = DataObject::get_one('League','ID='.$leagueId);
        $league->updateTable();
        
        return $this->getLeagueTable($league->SeasonID,true);
    }
    
    public function addFairPlayFoulPointsToTeam($request) {
        $leagueId = intval($_POST['leagueId']);
        $teamId = intval($_POST['teamId']);
        $fp = new FairPLayFoulPoint();
        $fp->TeamID = $teamId;
        $fp->LeagueID = $leagueId;
        $fp->points = $_POST['points'];
        $fp->desc = $_POST['desc'];
        $fp->write();
        $league = DataObject::get_one('League','ID='.$leagueId);
        $league->updateTable();
        
        return $this->getLeagueTable($league->SeasonID,true);
    }
    
    public function deleteFoulPoint($request) {
    	$fpId = intval($_POST['FoulPointId']);
    	$fp = DataObject::get_one('FoulPoint','ID='.$fpId);
        $leagueId = $fp->LeagueID;
        $fp->delete();
        $league = DataObject::get_one('League','ID='.$leagueId);
        $league->updateTable();
        
        return $this->getLeagueTable($league->SeasonID,true);
    }
    public function deleteFairPlayPoint($request) {
        $fpId = intval($_POST['FoulPointId']);
        $fp = DataObject::get_one('FairPlayFoulPoint','ID='.$fpId);
        $leagueId = $fp->LeagueID;
        $fp->delete();
        $league = DataObject::get_one('League','ID='.$leagueId);
        $league->updateTable();
        
        return $this->getLeagueTable($league->SeasonID,true);
    }
}
?>